const {
  PushProxy,
  TaskProxy,
  HistoryProxy
} = require('../proxys')

async function start () {
  const task = await TaskProxy.findNextTask()
  if (task) {
    const result = await PushProxy.push(task)
    const { status, data } = result
    if (status === 200) {
      process.send({
        code: 0,
        data
      })
      const tmp = data.sendno === '0' ? 0 : -1
      const history = {
        time: Date.now(),
        name: task.name,
        songs_id: task.id,
        msg_id: data.msg_id,
        status: tmp
      }
      await TaskProxy.deleteById(task._id)
      await HistoryProxy.newAndSave(history)
    } else {
      process.send({
        code: -1,
        msg: '推送失败'
      })
    }
  } else {
    process.send({
      code: 1,
      msg: '推送队列已为空'
    })
  }
}

start()
